/*
 * ========================================================
 * Copyright(c) 2014 杭州偶尔科技-版权所有
 * ========================================================
 * 本软件由杭州偶尔科技所有, 未经书面许可, 任何单位和个人不得以
 * 任何形式复制代码的部分或全部, 并以任何形式传播。
 * 公司网址
 *
 * 			http://www.kkkd.com/
 *
 * ========================================================
 */
package com.simon.mengkou.ui.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.ouertech.android.agm.lib.base.future.core.AgnettyFuture;
import com.ouertech.android.agm.lib.base.future.core.AgnettyResult;
import com.ouertech.android.agm.lib.base.utils.UtilList;
import com.simon.mengkou.R;
import com.simon.mengkou.data.bean.base.Product;
import com.simon.mengkou.data.bean.base.Shop;
import com.simon.mengkou.data.bean.base.Store;
import com.simon.mengkou.future.base.OuerFutureListener;
import com.simon.mengkou.system.global.OuerApplication;
import com.simon.mengkou.system.global.OuerDispatcher;
import com.simon.mengkou.ui.activity.CartNewActivity;
import com.simon.mengkou.utils.UtilOuer;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * @author : Zhenshui.Xia
 * @since : 2016/3/24.
 * desc    : 购物车商品适配器
 */
public class CartNewProductAdapter extends AbsAdapter<Product> {
    private CartNewAdapter mAdapter;
    private CartNewActivity mActivity;
    private String mItemId;

    public CartNewProductAdapter(Context context, CartNewActivity activity, CartNewAdapter adapter) {
        super(context);
        this.mAdapter = adapter;
        this.mActivity = activity;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup arg2) {
        final Holder holder;
        if (null == convertView) {
            convertView = mInflater.inflate(R.layout.layout_cart_new_product_item, null);
            holder = new Holder();
            convertView.setTag(holder);
            ButterKnife.bind(holder, convertView);
        } else {
            holder = (Holder) convertView.getTag();
        }

        final Product data = getItem(position);
        if(!mAdapter.isEditable()) {
//            holder.mIvSelect.setVisibility(View.GONE);
            holder.mTvCount.setVisibility(View.VISIBLE);
            holder.mLlCountRoot.setVisibility(View.GONE);

            //数量
            holder.mTvCount.setText(mContext.getString(R.string.common_multi, data.getCount()));
        } else {
//            holder.mIvSelect.setVisibility(View.VISIBLE);
            holder.mTvCount.setVisibility(View.GONE);
            holder.mLlCountRoot.setVisibility(View.VISIBLE);

            //数量
            holder.mTvEditCount.setText(String.valueOf(data.getCount()));
            holder.mIvAdd.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                        final int num = data.getCount() + 1;
                        AgnettyFuture future = OuerApplication.mOuerFuture.cartEdit(
                                data.getId(), num, data.getOrderItemId(),
                                new OuerFutureListener(mContext) {
                                    @Override
                                    public void onStart(AgnettyResult result) {
                                        super.onStart(result);
                                        mActivity.setWaitingDialog(true);
                                    }

                                    @Override
                                    public void onComplete(AgnettyResult result) {
                                        super.onComplete(result);
                                        mActivity.setWaitingDialog(false);
                                        data.setCount(num);
                                        holder.mTvCount.setText(mContext.getString(R.string.common_multi, data.getCount()));
                                        holder.mTvEditCount.setText(String.valueOf(data.getCount()));
                                        OuerDispatcher.editCartBroadcast(mContext);


                                        int size = OuerApplication.mPreferences.getCartProductNum() + 1;
                                        OuerApplication.mPreferences.setCartProductNum(size);
                                        OuerDispatcher.sendAddCartBroadcast(mActivity, size);

                                    }

                                    @Override
                                    public void onException(AgnettyResult result) {
                                        super.onException(result);
                                        mActivity.setWaitingDialog(false);
                                        UtilOuer.showExceptionToast(mContext, result, R.string.cart_edit_count_fail);
                                    }

                                    @Override
                                    public void onNetUnavaiable(AgnettyResult result) {
                                        super.onNetUnavaiable(result);
                                        mActivity.setWaitingDialog(false);
                                    }
                                });

                        mActivity.attachDestroyFutures(future);
                }
            });

            holder.mIvReduce.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    final int num = data.getCount() - 1;
                    if(num <= 0) {
                        return;
                    }

                    AgnettyFuture future = OuerApplication.mOuerFuture.cartEdit(
                            data.getId(), num, data.getOrderItemId(),
                            new OuerFutureListener(mContext) {
                                @Override
                                public void onStart(AgnettyResult result) {
                                    super.onStart(result);
                                    mActivity.setWaitingDialog(true);
                                }

                                @Override
                                public void onComplete(AgnettyResult result) {
                                    super.onComplete(result);
                                    mActivity.setWaitingDialog(false);
                                    data.setCount(num);

                                    holder.mTvCount.setText(mContext.getString(R.string.common_multi, data.getCount()));
                                    holder.mTvEditCount.setText(String.valueOf(data.getCount()));
                                    OuerDispatcher.editCartBroadcast(mContext);

                                    int size = OuerApplication.mPreferences.getCartProductNum() - 1;
                                    OuerApplication.mPreferences.setCartProductNum(size);
                                    OuerDispatcher.sendAddCartBroadcast(mActivity, size);
                                }

                                @Override
                                public void onException(AgnettyResult result) {
                                    super.onException(result);
                                    mActivity.setWaitingDialog(false);
                                    UtilOuer.showExceptionToast(mContext, result, R.string.cart_edit_count_fail);
                                }

                                @Override
                                public void onNetUnavaiable(AgnettyResult result) {
                                    super.onNetUnavaiable(result);
                                    mActivity.setWaitingDialog(false);
                                }
                            });

                    mActivity.attachDestroyFutures(future);
                }
            });
        }

        //是否选中
        if(data.isSelected()) {
            holder.mIvSelect.setImageResource(R.drawable.common_ic_select_pressed);
        } else {
            holder.mIvSelect.setImageResource(R.drawable.common_ic_select_normal);
        }

        holder.mIvSelect.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                data.setSelected(!data.isSelected());
                if(data.isSelected()) {
                    holder.mIvSelect.setImageResource(R.drawable.common_ic_select_pressed);
                } else {
                    holder.mIvSelect.setImageResource(R.drawable.common_ic_select_normal);
                }

                //全选了
                OuerDispatcher.sendCartSelectAllBroadcast(mContext, isSelectAll());
            }
        });

        //图片
        OuerApplication.mImageLoader.loadImage(holder.mSdvImage, data.getImage().getUrl());
        holder.mSdvImage.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                //打开商品详情
                if(!mAdapter.isEditable()) {
                    OuerDispatcher.presentProductDetailActivity(mContext, data.getId());
                }
            }
        });

        //标题
        holder.mTvTitle.setText(data.getTitle());
        //价格
        holder.mTvPrice.setText(mContext.getString(R.string.common_price,
                UtilOuer.formatPrice(data.getPrice())));

        return convertView;
    }


    /**
     * 是否已经全选
     */
    public boolean isSelectAll() {
        if(UtilList.isNotEmpty(mAdapter.getData())) {
            for(Store store : mAdapter.getData()) {
                for (Product p : store.getGoodsList()) {
                    if (!p.isSelected()) {
                        return false;
                    }
                }
            }
        }

        return true;
    }

    /**
     * @author zhenshui.xia
     */
    class Holder {
        //是否选中
        @Bind(R.id.order_id_select)
        ImageView mIvSelect;
        //图片
        @Bind(R.id.order_id_image)
        SimpleDraweeView mSdvImage;
        //标题
        @Bind(R.id.order_id_title)
        TextView mTvTitle;
        //价格
        @Bind(R.id.order_id_price)
        TextView mTvPrice;
        //数量
        @Bind(R.id.order_id_count)
        TextView mTvCount;
        //
        @Bind(R.id.order_id_count_root)
        LinearLayout mLlCountRoot;
        @Bind(R.id.order_id_add)
        ImageView mIvAdd;
        @Bind(R.id.order_id_reduce)
        ImageView mIvReduce;
        @Bind(R.id.order_id_edit_count)
        TextView mTvEditCount;
    }
}
